﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tanmia.Basis.Core;
using Tanmia.Basis.Domain;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System.ComponentModel;
using Microsoft.Practices.Prism.Regions;
using System.Windows.Data;
using System.Windows;

namespace Tanmia.Basis.ViewModel
{
    public abstract class BaseViewModel<T> : NotificationObject, IDataErrorInfo where T : BaseBusinessObject, new()
    {
        #region Properties

        //public int SelectedIndex
        //{
        //    get
        //    {
        //        return Entities.CurrentPosition;
        //    }
        //    set
        //    {
        //        Entities.MoveCurrentToPosition(value);
        //    }
        //}

        private T entity;

        protected BaseDomainObject<T> DomainObject { get; set; }

        public T Entity
        {
            get { return entity; }
            set
            {

                if (entity != null)
                    entity.PropertyChanged -= Entity_PropertyChanged;

                entity = value;
                entity.PropertyChanged += Entity_PropertyChanged;
                RaisePropertyChanged("Entity");
            }
        }

        public ICollectionView Entities { get; set; }

        #endregion

        public BaseViewModel(BaseDomainObject<T> domainObject)
        {
            Entity = new T();

            DomainObject = domainObject;
        }

        #region On

        public virtual bool SaveCore()
        {
            bool retVal = false;

            try
            {
                DomainObject.SaveOrUpdate(Entity);
                Refresh();
                retVal = true;
            }
            catch { throw; }

            return retVal;
        }

        public virtual void Save()
        {
            SaveCore();
        }

        public virtual void Refresh()
        {
            Entities = CollectionViewSource.GetDefaultView(DomainObject.Gets());
            RaisePropertyChanged(() => Entities);
        }

        public virtual void Delete()
        {
            try
            {
                DomainObject.Delete(Entities.CurrentItem as T);
                Refresh();
            }
            catch
            {
                throw;
            }
        }

        public virtual void Print() { }

        public virtual void PreviewPrint() { }

        public virtual void PrintSettings() { }

        public virtual void First()
        {
            Entities.MoveCurrentToFirst();

            RaisePropertyChanged("Entity");
        }

        public virtual void Previous()
        {
            Entities.MoveCurrentToPrevious();

            RaisePropertyChanged("Entity");
        }

        public virtual void Next()
        {
            Entities.MoveCurrentToNext();

            RaisePropertyChanged("Entity");
        }

        public virtual void Last()
        {
            Entities.MoveCurrentToLast();

            RaisePropertyChanged("Entity");
        }

        #endregion

        private void Entity_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            //((DelegateCommand)SaveCommand).RaiseCanExecuteChanged();
            //((DelegateCommand)SaveAndNewCommand).RaiseCanExecuteChanged();
        }

        #region IDataErrorInfo Members

        public string Error
        {
            get { return Entity.Error; }
        }

        public string this[string columnName]
        {
            get { return Entity[columnName]; }
        }

        #endregion
    }
}
